﻿using BellAssistant.Model.Tables;
using BellAssistant.WebAPI.Context;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace BellAssistant.WebAPI.BLL
{
    public class DataBLL
    {
        /// <summary>
        /// 保存商品信息
        /// </summary>
        /// <param name="good"></param>
        /// <returns></returns>
        public static bool SaveGoods(Goods good)
        {
            bool saveFlg = false;
            Goods gd = null;
            if (good != null)
            {
                if (good.GoodsID != 0)//修改
                {
                    gd = BellDataContext.GetInstance().Goods.GetData(string.Format("GoodsID={0}", good.GoodsID)).FirstOrDefault();
                }
                else //新增
                {
                    gd = new Goods();
                    int goodsID = BellDataContext.GetInstance().getListBySql<int>("select Goods.nextval from dual").FirstOrDefault();
                    gd.GoodsID = goodsID;
                }
            }
            #region 
            gd.ProductCategoryID = good.ProductCategoryID;
            gd.Source = good.Source;
            gd.URL = good.URL;

            gd.Name = good.Name;
            gd.PromotionID = good.PromotionID;
            gd.Cost = good.Cost;
            gd.Price = good.Price;
            gd.PromotionPrice = good.PromotionPrice;
            gd.StatusMean = good.StatusMean;
            gd.Status = good.Status;

            gd.BrandID = good.BrandID;
            gd.ExpressTypeID = good.ExpressTypeID;
            gd.Profit = good.Profit;
            gd.Stock = good.Stock;
            gd.ConsignmentPlace = good.ConsignmentPlace;

            gd.PromotionStart = good.PromotionStart;
            gd.PromotionEnd = good.PromotionEnd;
            gd.Shelves = good.Shelves;
            gd.OffShelves = good.OffShelves;
            gd.ExpressChipper = good.ExpressChipper;

            #endregion
            BellDataContext.GetInstance().Goods.AddAndSave(gd);

            Goods goodTemp = BellDataContext.GetInstance().Goods.GetData(string.Format("GoodsID={0}", gd.GoodsID)).FirstOrDefault();
            if (goodTemp != null)
            {
                saveFlg = true;
            }
            return saveFlg;
        }

        /// <summary>
        /// 获取所有销售商品
        /// </summary>
        /// <returns></returns>
        public static List<Goods> GetAllGoods()
        {
            List<Goods> list = BellDataContext.GetInstance().Goods.GetData().ToList();
            //关联几个表把Id转为名称显示到list
            return list;
        }


        /// <summary>
        /// 新增商品类别
        /// </summary>
        /// <param name="product"></param>
        /// <param name="newName"></param>
        /// <returns></returns>
        public static bool SaveCategory(ProductCategory product, string newName = "")
        {
            bool saveFlg = false;
            ProductCategory pd = null;
            if (product != null)
            {
                if (product.ProductID != 0)//修改
                {
                    if (IsExistsProductCategory(product, product.ProductID))
                    {
                        return false;
                    }
                    pd = BellDataContext.GetInstance().ProductCategory.GetData(string.Format("ProductID={0}", product.ProductID)).FirstOrDefault();
                    pd.CategoryName = newName;
                    pd._entityStatu = CDataContext.EEntityStatus.Changed;
                    BellDataContext.GetInstance().ProductCategory.save();
                }
                else //新增
                {
                    if (IsExistsProductCategory(product) || product.CategoryName == "")
                    {
                        return false;
                    }
                    pd = new ProductCategory();
                    pd.CategoryName = product.CategoryName;
                    pd.Status = true;
                    pd.StatusMean = pd.Status == true ? "正常" : "停用";
                    pd._entityStatu = CDataContext.EEntityStatus.Insert;
                    BellDataContext.GetInstance().ProductCategory.AddAndSave(pd);
                }
                ProductCategory pdTemp = BellDataContext.GetInstance().ProductCategory.GetData(string.Format("ProductID={0}", pd.ProductID)).FirstOrDefault();
                if (pdTemp != null)
                {
                    saveFlg = true;
                }
            }

            return saveFlg;
        }
        public static bool DelProductCategory(int id)
        {
            var pc = BellDataContext.GetInstance().ProductCategory.GetData(string.Format("ProductID={0}", id)).FirstOrDefault();
            if (pc != null)
            {
                pc.Status = false;
                pc.StatusMean = "停用";
                return true;
            }
            return false;
        }
        public static bool IsExistsProductCategory(ProductCategory pr, int? id = null)
        {
            var prtemp = false;
            if (id.HasValue && id.Value > 0)
            {
                prtemp = BellDataContext.GetInstance().ProductCategory.GetData(string.Format("CategoryName='{0}' and ProductID !={1}", pr.CategoryName, id.Value)).Any();
                return prtemp;
            }
            prtemp = BellDataContext.GetInstance().ProductCategory.GetData(string.Format("CategoryName='{0}'", pr.CategoryName)).Any();
            return prtemp;
        }

        /// <summary>
        /// 获取所有的商品类别
        /// </summary>
        /// <returns></returns>
        public static List<ProductCategory> GetAllProductCategory()
        {
            List<ProductCategory> list = new List<ProductCategory>();
            list = BellDataContext.GetInstance().ProductCategory.GetData().ToList();
            return list;
        }

        /// <summary>
        /// 根据商品类别名称获取类别ID
        /// </summary>
        /// <param name="categoryName"></param>
        /// <returns></returns>
        public static int GetProductCategoryIDByName(string categoryName)
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendFormat("select ProductID From ProductCategory where CategoryName='{0}'", categoryName);
            return BellDataContext.GetInstance().getListBySql<int>(sb.ToString()).FirstOrDefault();
        }

        /// <summary>
        /// 新增品牌
        /// </summary>
        /// <param name="brandName"></param>
        /// <returns></returns>
        public static string SaveBrand(string brandName)
        {
            string saveFlg = "";
            Brand temp = BellDataContext.GetInstance().Brand.GetData(string.Format("BrandName='{0}'", brandName)).FirstOrDefault();
            if (temp != null)
            {
                saveFlg = "已经存在该品牌名称,请换一个名称!";
            }
            else
            {
                temp = new Brand();
                temp.BrandName = brandName;
                temp.Status = true;
                temp.StatusMean = temp.Status == true ? "正常" : "停用";
                BellDataContext.GetInstance().Brand.AddAndSave(temp);
                saveFlg = "新增成功!";
            }
            return saveFlg;
        }

        /// <summary>
        /// 获取所有品牌
        /// </summary>
        /// <returns></returns>
        public static List<Brand> GetAllBrand()
        {
            List<Brand> list = new List<Brand>();
            list = BellDataContext.GetInstance().Brand.GetData().ToList();
            return list;
        }

        /// <summary>
        /// 根据品牌名称获取品牌ID
        /// </summary>
        /// <param name="brandName"></param>
        /// <returns></returns>
        public static int GetBrandIDByName(string brandName)
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendFormat("select BrandID From Brand where BrandName='{0}'", brandName);
            return BellDataContext.GetInstance().getListBySql<int>(sb.ToString()).FirstOrDefault();
        }

        /// <summary>
        /// 根据快递类型名称获取快递ID
        /// </summary>
        /// <param name="categoryName"></param>
        /// <returns></returns>
        public static int GetExpressTypeIDByName(string expressTypeName)
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendFormat("select ExpressTypeID From ExpressType where ExpressTypeName='{0}'", expressTypeName);
            return BellDataContext.GetInstance().getListBySql<int>(sb.ToString()).FirstOrDefault();
        }

        /// <summary>
        /// 根据促销类型名称获取促销ID
        /// </summary>
        /// <param name="categoryName"></param>
        /// <returns></returns>
        public static int GetPromotionIDByName(string promotionName)
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendFormat("select PromotionID From PromotionType where PromotionName='{0}'", promotionName);
            return BellDataContext.GetInstance().getListBySql<int>(sb.ToString()).FirstOrDefault();
        }

        /// <summary>
        /// 新增快递类别
        /// </summary>
        /// <param name="brandName"></param>
        /// <returns></returns>
        public static string SaveExpressType(ExpressType expressType)
        {
            string saveFlg = "";
            ExpressType temp = BellDataContext.GetInstance().ExpressType.GetData(string.Format("ExpressTypeName='{0}'", expressType.ExpressTypeName)).FirstOrDefault();
            if (temp != null)
            {
                saveFlg = "已经存在该快递类别名称,请换一个名称!";
            }
            else
            {
                temp = new ExpressType();
                temp.ExpressTypeName = expressType.ExpressTypeName;
                temp.ExpressFromAddress = expressType.ExpressFromAddress;
                temp.ExpressAtypePrice = expressType.ExpressAtypePrice;
                temp.ExpressBtypePrice = expressType.ExpressBtypePrice;
                temp.ExpressCtypePrice = expressType.ExpressCtypePrice;
                temp.ExpressDtypePrice = expressType.ExpressDtypePrice;
                temp.ExpressEtypePrice = expressType.ExpressEtypePrice;

                temp.Status = true;
                temp.StatusMean = temp.Status == true ? "正常" : "停用";
                saveFlg = "新增成功!";
                BellDataContext.GetInstance().ExpressType.AddAndSave(temp);
            }
            return saveFlg;
        }

        
        /// <summary>
        /// 获取所有的快递类别
        /// </summary>
        /// <returns></returns>
        public static List<ExpressType> GetAllExpressType()
        {
            List<ExpressType> list = new List<ExpressType>();
            list = BellDataContext.GetInstance().ExpressType.GetData().ToList();
            return list;
        }
        #region 促销管理

        /// <summary>
        /// 获取促销
        /// </summary>
        /// <returns></returns>
        public static List<PromotionType> GetAllPromotionType()
        {
            List<PromotionType> list = new List<PromotionType>();
            //list = BellDataContext.GetInstance().PromotionType.GetData().ToList();
            StringBuilder sb = new StringBuilder();
            sb.Append("select * from PromotionType");
            list = BellDataContext.GetInstance().getListBySql<PromotionType>(sb.ToString()).ToList();
            return list;
        }

        /// <summary>
        /// 新增促销类别
        /// </summary>
        /// <param name="promotionType"></param>
        /// <returns></returns>
        public static string SavePromotionType(PromotionType promotionType)
        {
            string saveFlg = "";
            var temp = BellDataContext.GetInstance().PromotionType.GetData(string.Format("PromotionName='{0}'", promotionType.PromotionName)).FirstOrDefault();
            if (temp != null)
            {
                saveFlg = "已经存在该活动名称,请换一个名称!";
            }
            else
            {
                temp = new PromotionType();
                temp.PromotionName = promotionType.PromotionName;
                temp.PromotionPeople = promotionType.PromotionPeople;
                temp.PromotionAction = promotionType.PromotionAction;
                temp.PromotionStatus = promotionType.PromotionStatus;
                temp.PromotionTag = promotionType.PromotionTag;
                temp.StartTime = promotionType.StartTime;
                temp.EndTime = promotionType.EndTime;
                temp.CouponID = promotionType.CouponID;
                saveFlg = "新增成功!";
                BellDataContext.GetInstance().PromotionType.AddAndSave(temp);
            }
            return saveFlg;
        }

        #endregion

    }
}